package com.renjiyue.dao;

import com.renjiyue.pojo.User;
import com.renjiyue.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;


public class testSelectUserById {
    @Test
    public void test123(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectUserById(1);
        System.out.println(user);

        session.clearCache();//手动清除缓存

        User user1 = mapper.selectUserById(1);
        System.out.println(user1);
        session.close();
    }
    @Test
    public void testQueryuSERb(){
        SqlSession session = MyBatisUtils.getSqlSession();
        SqlSession session1 = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        UserMapper mapper1 = session1.getMapper(UserMapper.class);

        User user = mapper.selectUserById(1);
        System.out.println(user);
        session.close();

        User user1 = mapper1.selectUserById(1);
        System.out.println(user1);
        System.out.println(user==user1);
        session1.close();
    }
    @Test
    public void test(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(4, "yuweishui", "123456");
        int i = mapper.addUser(user);
        System.out.println(i);
        //提交事务，重点！不写的话不会提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void Test2(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User(4, "yuweishui1", "111111");
        int i = mapper.updateUser(user);
        System.out.println(i);
        session.commit();
        session.close();
    }
    @Test
    public void test3(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.deleteUser(4);
        System.out.println(i);
        session.commit();
        session.close();
    }
    @Test
    public void test4(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectUserById(1);
        System.out.println(user);
        session.close();
    }
    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        for (User user : mapper.getAllUser()) {
            System.out.println(user);
        }
    sqlSession.close();
    }
    @Test
    public void testSelectUserById1(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectUserById(2);
        System.out.println(user);
        session.close();
    }
    @Test
    public void testAddUser(){
        User user = new User(4,"huxiaolong1","123456");
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.addUser2(user);
        session.close();
    }
    @Test
    public void testDeleteser(){
        SqlSession session = MyBatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteUser3(4);
        session.close();
    }
}
